#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"

//https://leetcode.cn/problems/implement-queue-using-stacks/submissions/415526844/
typedef struct {
    Stack spush;
    Stack spop;
} MyQueue;


MyQueue* myQueueCreate() {
    MyQueue* obj = (MyQueue*)malloc(sizeof(MyQueue));
    StackInit(&obj->spush);
    StackInit(&obj->spop);
    return obj;
}

void myQueuePush(MyQueue* obj, int x) {
    StackPush(&obj->spush, x);
}

int myQueuePop(MyQueue* obj) {
    if (StackEmpty(&obj->spop)) {
        while (!StackEmpty(&obj->spush)) {
            StackPush(&obj->spop, StackTop(&obj->spush));
            StackPop(&obj->spush);
        }
    }
    int ret = StackTop(&obj->spop);
    StackPop(&obj->spop);
    return ret;
}

int myQueuePeek(MyQueue* obj) {
    if (StackEmpty(&obj->spop)) {
        while (!StackEmpty(&obj->spush)) {
            StackPush(&obj->spop, StackTop(&obj->spush));
            StackPop(&obj->spush);
        }
    }
    return StackTop(&obj->spop);
}

bool myQueueEmpty(MyQueue* obj) {
    return StackEmpty(&obj->spop) && StackEmpty(&obj->spush);
}

void myQueueFree(MyQueue* obj) {
    StackDestory(&obj->spop);
    StackDestory(&obj->spush);
    free(obj);
}

int main() {
    MyQueue* p = myQueueCreate();
    myQueuePush(p, 1);
    myQueuePush(p, 1);
    myQueuePeek(p);
    myQueuePop(p);
    myQueuePop(p);
    myQueueEmpty(p);
    myQueueFree(p);
    return 0;
}
